import { baseUrl } from '@/config/env'
import { getToken } from '@/utils/auth'
import { Message } from 'element-ui'

// ------------------ 导入&上传 ----------------------
// 内部上传文件--导入用
export const commonUpload = async function(url, data) {
    let request_url = baseUrl + url;
    const formData = new FormData();
    for (const key in data) {
        const value = data[key];
        formData.append(key, value)
    }

    const response = await fetch(request_url,  {
        method: 'POST',
        headers: {
            TOKEN: getToken()
        },
        body: formData, // 将 FormData 作为请求体
    })
    const responseJson = await response.json();
    let code = responseJson.code
    if (code == 200){
        return responseJson
    }else{
        Message({
            message: responseJson.data.msg,
            type: 'error'
        });
        return Promise.reject('error')
    }
}